<template>
  <div class="login-page">
    <div class="center-box">
      <div class="left-box">
        <img src="../../assets/images/login-left.jpg" alt=""></img>
      </div>
      <div class="right-box">
        <div class="top">计算机应用技术3班</div>
        <div class="center">
           <el-input
            v-model="username"
            placeholder="请输入用户名"
            :prefix-icon="User"
          />
          <el-input
            v-model="password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
          />
          <el-button type="primary" :loading="loading" @click="loginAction">登 录</el-button>
        </div>
        <div class="bottom">
          <span>还没有账号？</span>
          <span @click="$router.push('/register')">去注册</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { login } from '@/api/index';
import storage from '@/utils/storage';
import { Lock, User } from '@element-plus/icons-vue';
import { ref } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter()
const username = ref('')
const password = ref('')
const loading = ref(false)

const loginAction = () => {
  loading.value = true
  console.log(username.value, password.value);
  if (!username.value || !password.value) {
    alert('请输入用户名和密码')
    return
  }
  // axios.post('http://152.136.202.150:3000/api/auth/login', {
  //   username: username.value,
  //   password: password.value
  // }).then(res => {
  //   console.log(res);
  //   const token = res.data.token
  //   const userInfo = res.data.user
  //   const menus = res.data.menus

  //   // localStorage.setItem('token', token)
  //   storage.set("token", token)
  //   storage.set("userInfo", userInfo)
  //   storage.set("menus", menus)

  //   loading.value = false
  // }).catch(err => {
  //   loading.value = false
  //   console.log(err);
  //   if (err.response) {
  //     ElMessage.error(err.response.data.message)
  //   }
  // })
  login({
    username: username.value,
    password: password.value
  }).then(res => {
    console.log(res);
    const token = res.token
    const userInfo = res.user
    const menus = res.menus
    // localStorage.setItem('token', token)
    storage.set("token", token)
    storage.set("userInfo", userInfo)
    storage.set("menus", menus)

    // 跳转到首页
    router.push('/home')
  }).catch(err => {
    loading.value = false
    console.log(err);
  })
}


</script>

<style lang="scss" scoped>
// background-color：背景颜色（如 #fff、rgba(0,0,0,0.5)）
// background-image：背景图片（如 url(...)）
// background-repeat：图片重复方式（repeat/no-repeat/repeat-x/repeat-y）
// background-position：图片位置（如 center、top left、50% 50%）
// background-size：图片大小（如 cover、contain、100% 100%）
// background-attachment：滚动行为（scroll/fixed/local）
// background-origin/background-clip：背景定位 / 绘制区域（较少用）
.login-page {
  width: 100vw;
  height: 100vh;
  background-image: url(../../assets/images/login-bg.png);
  background-repeat: no-repeat;
  background-position: center center;
  background-size: 100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .center-box {
    width: 800px;
    height: 400px;
    display: flex;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 3px 4px rgba(0, 0, 0, 0.2);
    &:hover {
      transform: scale(1.02);
      box-shadow: 0 3px 4px rgba(45, 10, 125, 0.4);
    }
    .left-box {
      flex: 1;
      img {
        height: 100%;
      }
    }
    .right-box {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      background-color: #fafafa;
      padding: 16px;
      .top {
        flex: 1;
        text-align: center;
        padding: 16px 0;
        font-size: 24px;
        font-weight: 700;
        color: #409EFF;
      }
      .center {
        flex: 4;
        display: flex;
        flex-direction: column;
        justify-content: space-evenly;
      }
      .bottom {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        span {
          margin: 0 8px;
          font-size: 12px;
          color: #999;
          &:last-child {
            color: #409EFF;
            cursor: pointer;
          }
        }
      }
    }
  }
}
</style>